package com.qf.java2201.web;

import com.qf.java2201.pojo.HOrder;
import com.qf.java2201.pojo.HUser;
import com.qf.java2201.service.OrderService;
import com.qf.java2201.service.impl.OrderServiceImpl;
import com.qf.java2201.utils.DateUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

/**
 * @date 2022/4/9
 * @desc  用户 点 现在预约 发送请求  保存预约单信息到数据库
 */
@WebServlet("/addOrder")
public class AddOrderServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 接受 预约单 信息

        String edid = req.getParameter("edid");  // 预约单医生id
        String odate = req.getParameter("odate"); // 预约日期
        String sjdid = req.getParameter("sjdid"); // 时间段id
        String sickness = req.getParameter("sickness"); // 病情
        String dmoney = req.getParameter("dmoney"); // 挂号费

        // TODO

        HttpSession session = req.getSession();

        HUser hUser = (HUser) session.getAttribute("u");

        HOrder hOrder = new HOrder();

        hOrder.setEdid(Integer.parseInt(edid));  // 封装医生的 did

        //将预约日期字符串 转为 日期类型
        Date date = DateUtil.strToDate(odate,"yyyy-MM-dd");

        hOrder.setOdate(new Timestamp(date.getTime()));

        hOrder.setSjdid(Integer.parseInt(sjdid));
        hOrder.setSickness(sickness);
        hOrder.setGhf(Double.parseDouble(dmoney));

        hOrder.setUdid(hUser.getUi().getDid());  // 封装当前用户的 did

        hOrder.setYyztid(21);


        OrderService orderService = new OrderServiceImpl();

        int oid = orderService.addOrder(hOrder);

        //添加成功  把预约单 信息展示在  pay.jsp 上面

        hOrder.setOid(oid);

        if(oid>0){
            session.setAttribute("oo",hOrder);
            resp.sendRedirect("pay.jsp");
        }else{

            resp.sendRedirect("xq.jsp");
        }


    }
}
